package net.sapfree.display.tags;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

import net.sapfree.common.Constants;
import net.sapfree.core.db.DBConnectionProvider;
import net.sapfree.core.db.DataBaseExecutor;
import net.sapfree.core.db.Record;
import net.sapfree.core.db.RecordSet;
import net.sapfree.display.tags.html.LinkTag;
import net.sapfree.display.tags.util.ResponseUtils;

public class ArticleTag extends LinkTag{
	private String artId;
	private Record r=null;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public int doStartTag() throws JspException {
		
		
		if(artId==null||"".equals(artId)){//如果id为空，则从quanList中取
			ArticleListTag artList = (ArticleListTag) pageContext.getAttribute("ArticleListTag");
			r=(Record)artList.getCurrent();
		}else{
			Connection conn=null;
			try {
				conn = DBConnectionProvider.getConnection(Constants.JNDI_SAPFREE);
				DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
				RecordSet articleList = new RecordSet();
				articleList=dbe.find(ArticleSql());
				if(articleList!=null&&articleList.size()>0){
					r=articleList.get(0);
				}
			} catch (SQLException e) {
				log.error("根据主键查找文章时出错，",e);
			}
		}
		pageContext.setAttribute("articleId", r.getString("AI_UUID",null));
		pageContext.setAttribute("ArticleTag", this);
		return EVAL_BODY_INCLUDE;
	}
	
	
	private String ArticleSql(){
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT AI.*,AC.* FROM ARTICLEINFO AI LEFT JOIN ARTICLECONTENT AC ON AI.AI_UUID = AC.AC_ARTICLEUUID  " );
		sql.append(" WHERE AI.AI_UUID='").append(artId).append("'");
		return sql.toString();
	}
	
	
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	/**
	 * @return the artName
	 */
	public String getArtId() {
		return artId;
	}

	/**
	 * @param artName the artName to set
	 */
	public void setArtId(String artId) {
		this.artId = artId;
	}
	
	
	/**
	 * @return the artName
	 */
	public Record getRecord() {
		return r;
	}

	/**
	 * @param artName the artName to set
	 */
	public void setRecord(Record _r) {
		this.r = _r;;
	}
	
}
